package com.kaibes.module.token.service;

import org.springframework.stereotype.Service;

import com.kaibes.module.token.pojo.TokenLog;
import com.kaibes.module.token.pojo.TokenLogType;
import com.kaibes.orm.core.core.Page;
import com.kaibes.orm.curd.QueryBuilder;
import com.kaibes.orm.curd.SelectBuilder;
import com.kaibes.web.api.ResponseServiceImpl;

@Service
public class TokenLogService extends ResponseServiceImpl<TokenLog> {
    
    public Page<TokenLog> pageByNicknameLike(Integer current, Integer size, String search) {
        QueryBuilder builder = QueryBuilder.newBuilder().inSelect("user_id",
                SelectBuilder.newBuilder().setTableName("user").addColumn("id")
                        .addQuery(QueryBuilder.newBuilder().like("nickname", search).build()).build());
        return page(current, size, builder.build());
    }
    
    public Page<TokenLog> pageByType(Integer current, Integer size, TokenLogType type) {
        QueryBuilder builder = QueryBuilder.newBuilder().inSelect("user_id",
                SelectBuilder.newBuilder().setTableName("user").addColumn("id")
                .addQuery(QueryBuilder.newBuilder().eq("type", type).build()).build());
        return page(current, size, builder.build());
    }
    
    public Page<TokenLog> pageByTypeOrNicknameLike(Integer current, Integer size, String search) {
        if (search.equals(TokenLogType.CREATE.getText())) {
            return pageByType(current, size, TokenLogType.CREATE);
        } else if (search.equals(TokenLogType.DELETE.getText())) {
            return pageByType(current, size, TokenLogType.DELETE);
        } else {
            return pageByNicknameLike(current, size, search);
        }
    }
    
}
